<template>
  <div>
    <div>
      <h4 style="margin:0 0 10px 0"> 商品标签 </h4>
      <el-row style="margin-bottom:10px;display:flex;align-items:center">
        <el-col :span="8" >
          <el-button type="primary" style="padding:8px 4px" @click="dialogVisible = true">添加标签</el-button>
        </el-col>
        <el-col :span="16" style="text-align:right">
          <el-input v-model="sheachValue" style="width:300px" placeholder="输入搜索内容"></el-input>
          <el-button type="primary" @click="seachFun">搜索</el-button>
        </el-col>
      </el-row>
      
      <el-table :data="tableData" border  style="width: 100%" >
        <el-table-column prop="name" label="序号" width="60">
          <template slot-scope="scope">{{scope.$index+1}}</template>
        </el-table-column>
        
        <el-table-column prop="name" label="标签名"> </el-table-column>      
        <el-table-column prop="color" label="色值"> </el-table-column>      
        <el-table-column prop="page_view" label="商品数"> </el-table-column>      
        
        <el-table-column label="操作">
          <template slot-scope="scope" >
            <div>
              <el-button class="addBtn" @click="editFun(scope.row)" v-if="userRole.KW_301002"> 编辑</el-button>
              <el-button class="addBtn" type="danger"   @click="deletData(scope.row.id)" v-if="userRole.KW_301003"> 删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
     
     <el-dialog :title="title" :visible.sync="dialogVisible" center width="40%" :before-close="handleClose">
        <el-form label-position="left" label-width="80px" ref="ruleForm2" :model="ruleForm2" :rules="rules2">
          <el-form-item label="标签名：" prop="labelName">
            <div style="display:flex;align-items:center">
              <el-input v-model="ruleForm2.labelName" style="width:300px"></el-input>
            </div>
          </el-form-item>
          <el-form-item label="色值：" prop="color">
            <div style="display:flex;align-items:center">
              <el-input v-model="ruleForm2.color" style="width:300px"></el-input>
            </div>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submitFun('ruleForm2')">确 定</el-button>
        </span>
      </el-dialog>
     
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import Http from '@/Http/Http'

  export default {
    data() { 
     
      var labelName = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入标签名'));
        } else {
          callback();
        }
      };
      var color = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入色值'));
        } else {
          callback();
        }
      };
      return {
        userRole:JSON.parse(sessionStorage.getItem('userSolt')),
        currentPage4: 1,//分野  
        total:0,      
        tableData: [],
        page:1,
        dialogVisible: false,
        editId:'',
        title:'添加标签',
        pageSize:10,
        sheachValue:'',
        formLabelAlign: {
          name: '',
          region: '',
          type: ''
        },
        ruleForm2: {
          labelName: '',
          color:''
        },
        rules2: {
          labelName:[
             { validator: labelName, trigger: 'blur' }
          ],
          color:[
             { validator: color, trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getlist()
    },
    methods: {
       getlist() {
        Http.commoTags({})
        .then( res => {
          this.tableData = res.data.data;
          this.total = res.data.data.total;
          
        })
        .catch( error => {
          
        })
      },  
       handleClose() {
        this.title = '添加标签';
        
        this.dialogVisible = false;
        this.ruleForm2.labelName = '';
        this.ruleForm2.color = '';
       
      },
      submitFun(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (valid) {
              const param = {
                "name":this.ruleForm2.labelName,
                "color":this.ruleForm2.color
              };
              this.title == '添加标签' ? this.addDataFun(param) : this.editDataFun(param);
            }
          }
        })
      },
      editFun(val) {
        this.title = '修改标签';
        this.dialogVisible = true;
        this.editId = val.id;
        this.ruleForm2.labelName = val.name;
        this.ruleForm2.color = val.color;
      },
      addDataFun(param) {
        Http.commoAddTags(param)
        .then(res => {
          if( res.data.code == 200 ){
            this.$message.success('添加成功')
            this.dialogVisible = false;
            this.getlist()
            this.ruleForm2.labelName = '';
            this.ruleForm2.color = '';
          } else if(res.data.code == -1003) {
            this.$message.error(`添加失败，原因：${res.data.message}`)
          }
        })
        .catch( error => {
          this.$message.error('服务器请求失败')
        })
      },
      editDataFun(param) {
        
        param.id = this.editId;
        Http.commoEditTags(param)
        .then(res => {
          if( res.data.code == 200 ){
            this.$message.success('修改成功')
            this.dialogVisible = false;
            this.getlist()
            this.ruleForm2.labelName = '';
            this.ruleForm2.color = '';
          } else if(res.data.code == -1003) {
            this.$message.error(`修改失败，原因：${res.data.message}`)
          }
        })
        .catch( error => {
          this.$message.error('服务器请求失败')
        })
      },
      deletData(row) {
        
        this.$confirm(`确认删除此标签吗？`)
        .then(_ => {
          
          Http.deletType(row)
          .then( res => {
            
            if( res.data.code == 200 ){
              this.$message.success('删除成功')
              this.getlist()         
            } else if(res.data.code == -1003) {
              this.$message.error(`请求失败原因：${res.data.message}`)
              this.$router.push('/')
            }
          })
          .catch( error => {
            this.$message.error('服务器请求失败')
          })
        })
        .catch(_ => {
          //取消部分
          
        });
      },
     
      
       //分页
      handleSizeChange(val) {
        this.pageSize = val;
        this.getlist()
        
      },
      handleCurrentChange(val) {
        this.page = val;
        this.getlist()
        
      },
     
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.editId ? this.editKeywordFun():this.addKeywordFun();
             this.editId ='';
             this.ruleForm2.keyword ='';
          } else {
            return false;
          }
        });
      },
      addKeywordFun(){
       
        Http.addKeyword({"keyword":this.ruleForm2.keyword})
        .then( res => {
          
          if( res.data.code == 200 ) { 
            this.$message.success('关键字添加成功')
            
            this.getlist()    
          } else if (res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
            setTimeout(()=>{
              this.$router.push('/')
            },2000)
          } else {
            this.$message.error('关键字添加失败')              
          }
        })
        .catch( error => {
          this.$message.error('服务器请求失败')
          
        })
      },
      editKeywordFun(){
        Http.editKeyword({"keyword":this.ruleForm2.keyword,'id':this.editId})
        .then( res => {
          
          if( res.data.code == 200 ) { 
            this.$message.success('关键字修改成功')
            
            this.getlist()    
          } else if (res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
            setTimeout(()=>{
              this.$router.push('/')
            },2000)
          } else {
            this.$message.error('关键字修改失败')              
          }
        })
        .catch( error => {
          this.$message.error('服务器请求失败')
          
        })
      },
      seachFun(){
        this.getlist()
      }
    }
  }
</script>